<?php

class m111123_082620_account_token_initial extends CDbMigration {
	public function safeUp() {
		$this
			->createTable('erp_account_token_initial',
				array(
					'id' => 'pk',
					'projectId' => 'int',
					'categoryId' => 'int',
					'debit' => 'decimal(16,2)',
					'credit' => 'decimal(16,2)',
					'createTime' => 'datetime',
					'createUserId' => 'int',
					'updateTime' => 'datetime',
					'updateUserId' => 'int',
				));
		$this
			->createIndex("account_token_initial_create_user_id", 'erp_account_token_initial',
				'createUserId');
		$this
			->addForeignKey('fk_account_token_initial_create_user_id', 'erp_account_token_initial',
				'createUserId', 'erp_user', 'id', 'SET NULL', 'CASCADE');
		$this
			->createIndex("account_token_initial_update_user_id", 'erp_account_token_initial',
				'updateUserId');
		$this
			->addForeignKey('fk_account_token_initial_update_user_id', 'erp_account_token_initial',
				'updateUserId', 'erp_user', 'id', 'SET NULL', 'CASCADE');

		$this
			->createIndex("account_token_initial_category_id", 'erp_account_token_initial',
				'categoryId');
		$this
			->addForeignKey('fk_account_token_initial_category_id', 'erp_account_token_initial',
				'categoryId', 'erp_account_token_category', 'id', 'CASCADE', 'CASCADE');
		$this
			->createIndex("account_token_initial_project_id", 'erp_account_token_initial',
				'projectId');
		$this
			->addForeignKey('fk_account_token_initial_project_id', 'erp_account_token_initial',
				'projectId', 'erp_account_token_project', 'id', 'CASCADE', 'CASCADE');

		$this->addColumn('erp_account_token_category', 'common', 'boolean');
		$this->execute('update erp_account_token_category set common=0;');
	}

	public function safeDown() {
		$this->dropTable('erp_account_token_initial');
		$this->dropColumn('erp_account_token_category', 'common');
	}
}
